package cn.zhaoguang.usedcar.main.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.zhaoguang.usedcar.common.http.ResultVO;
import cn.zhaoguang.usedcar.common.page.PageRequest;
import cn.zhaoguang.usedcar.common.page.PageResult;
import cn.zhaoguang.usedcar.common.util.FileHandleUtil;
import cn.zhaoguang.usedcar.main.entity.Usedcar;
import cn.zhaoguang.usedcar.main.entity.dto.UsedcarDetailDTO;
import cn.zhaoguang.usedcar.main.entity.vo.CollectionVO;
import cn.zhaoguang.usedcar.main.service.CollectionService;
import cn.zhaoguang.usedcar.main.service.UsedcarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Map;

/**
 * @Description
 * @Author zhaoguang
 * @Date 2021/7/3 11:05
 */
@RestController
@RequestMapping("/usedcar")
@Api(tags = "车辆控制器")
public class UsedcarController {

    @Autowired
    private UsedcarService usedcarService;

    @Autowired
    private CollectionService collectionService;

    @PostMapping("/saveImg")
    @ApiOperation(value = "商家接口-保存图片并返回图片URL", notes = "保存图片并返回图片URL")
    public ResultVO saveImgList(String sellerId, MultipartFile[] files) {
        //保存到本机服务器
        String date = DateUtil.format(DateUtil.date(), "yyyy/MM/");
        File folder = new File("F:/Project/usedcar/car-img/" + date + sellerId);

        //设置能够访问图片的URL
        StringBuffer sb = new StringBuffer();
        for (MultipartFile f : files) {
            String fileName = FileHandleUtil.saveFile(folder, f);
            String URL = "http://localhost:8090/usedcar/car/img/" + date + sellerId + "/" + fileName + ",";
            sb.append(URL);
        }
        //处理末尾的逗号
        String str = StrUtil.removeSuffix(sb.toString(), ",");
        return ResultVO.success("添加成功", str);
    }

    @PostMapping("/add")
    @ApiOperation(value = "商家接口-添加车辆", notes = "添加车辆")
    public ResultVO add(@RequestBody Usedcar usedcar) {
        int result = usedcarService.add(usedcar);
        if (result > 0) {
            return ResultVO.success("添加成功");
        }
        return ResultVO.error("添加失败");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "商家接口-删除车辆", notes = "删除车辆")
    public ResultVO delete(@PathVariable("id") Integer id) {
        int result = usedcarService.delete(id);
        if (result > 0) {
            return ResultVO.success("删除成功");
        }
        return ResultVO.error("删除失败");
    }

    @PutMapping("/update")
    @ApiOperation(value = "商家接口-修改车辆信息", notes = "修改车辆信息")
    public ResultVO update(@RequestBody Usedcar usedcar) {
        int result = usedcarService.update(usedcar);
        if (result > 0) {
            return ResultVO.success("修改成功");
        }
        return ResultVO.error("修改失败");
    }

    @PostMapping("/findAllBySellerId")
    @ApiOperation(value = "商家接口-查询本店铺全部车辆", notes = "查询本店铺全部车辆")
    public ResultVO findAllBySellerId(@RequestBody PageRequest pageRequest) {
        PageResult result = usedcarService.findAllBySellerId(pageRequest);
        return ResultVO.success("查询成功", result);
    }

    @PostMapping("/findDetailByIdForSeller")
    @ApiOperation(value = "商家接口-查询车辆详情、店铺信息", notes = "查询车辆详情、店铺信息")
    public ResultVO findDetailByIdForSeller(@RequestBody Map<String, Integer> map) {
        //车辆详情和店铺信息
        UsedcarDetailDTO detail = usedcarService.findDetailById(map.get("usedcarId"));

        return ResultVO.success("查询成功", detail);
    }

    @PostMapping("/findUsedcarById")
    @ApiOperation(value = "商家接口-根据车辆id查询车辆(修改车辆查询时使用)", notes = "根据车辆id查询车辆(修改车辆查询时使用)")
    public ResultVO findUsedcarById(@RequestBody Map<String, Integer> map) {
        Integer id = map.get("id");
        Usedcar car = usedcarService.findUsedcarById(id);
        return ResultVO.success("查询成功", car);
    }

    @PostMapping("/findAll")
    @ApiOperation(value = "用户接口-查询全部车辆", notes = "查询全部车辆")
    public ResultVO findAll(@RequestBody PageRequest pageRequest) {
        PageResult result = usedcarService.findAll(pageRequest);
        return ResultVO.success("查询成功", result);
    }

    @PostMapping("/choiceCar")
    @ApiOperation(value = "用户接口-搜索、筛选车辆", notes = "搜索、筛选车辆")
    public ResultVO choiceCar(@RequestBody PageRequest pageRequest) {
        PageResult result = usedcarService.choiceCar(pageRequest);
        return ResultVO.success("查询成功", result);
    }

    @PostMapping("/findDetailByIdForUser")
    @ApiOperation(value = "用户接口-查询车辆详情、店铺信息、收藏信息", notes = "查询车辆详情、店铺信息、收藏信息")
    public ResultVO findDetailByIdForUser(@RequestBody Map<String, Integer> map) {
        Integer usedcarId = map.get("usedcarId");
        Integer userId = map.get("userId");

        //车辆详情和店铺信息
        UsedcarDetailDTO detail = usedcarService.findDetailById(usedcarId);

        //收藏功能
        Integer exist = collectionService.findCollectionByUserId(userId, usedcarId);
        CollectionVO collection = new CollectionVO();
        if (exist != null) {
            collection.setId(exist);
            collection.setSign("true");
            detail.setCollection(collection);
        } else {
            collection.setId(exist);
            collection.setSign("false");
            detail.setCollection(collection);
        }
        return ResultVO.success("查询成功", detail);
    }

    @PostMapping("/findSellerInfoAndUsedcarByIdForUser")
    @ApiOperation(value = "用户接口-查询某个商家的信息和车辆(用户进入某个店铺时使用的API)", notes = "查询某个商家的信息和车辆(用户进入某个店铺时使用的API)")
    public ResultVO findSellerInfoAndUsedcarByIdForUser(@RequestBody Map<String, Integer> map) {
        return ResultVO.success("查询成功", usedcarService.findSellerInfoAndUsedcarByIdForUser(map.get("id")));
    }

    @GetMapping("/findTodayUsedcar")
    @ApiOperation(value = "用户接口-查询今日添加的最新车辆", notes = "查询今日添加的最新车辆")
    public ResultVO findTodayUsedcar() {
        return ResultVO.success("查询成功", usedcarService.findTodayUsedcar());
    }





    @PostMapping("/findAllForManager")
    @ApiOperation(value = "管理员接口-查询全部车辆(与用户接口的查询全部车辆不同)", notes = "查询全部车辆(与用户接口的查询全部车辆不同)")
    public ResultVO findAllForManager(@RequestBody PageRequest pageRequest) {
        PageResult result = usedcarService.findAllForManager(pageRequest);
        return ResultVO.success("查询成功", result);
    }
}
